<!--
 * @Autor: curry
 * @Description: 
 * @Date: 2022-11-28 19:51:14
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr-left" @click="toLeft" :style="comStyle">&#xe6ef;</span>
    <span class="iconfont arr-right" @click="toRight" :style="comStyle">&#xe6ed;</span>
    <span class="name" :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";
export default {
  name: "Hot",

  data() {
    return {
      chartInstance: null,
      allData: null,
      startValue: 0, //区域缩放起点
      endValue: 9, //区域缩放终点
      timeId: null,
      currtIndex: 0, //当前展示的1级数据
      titleFontSize:0
    };
  },
  computed: {
    catName() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.currtIndex].name;
      }
    },
    comStyle(){
        return {
            fontSize:this.titleFontSize+'px',
            color:getThemeValue(this.theme).titleColor
        }
    },
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose()
      this.initChart();
      this.screenAdapter()
      this.updateChart()
    }
  },
  created () {
    this.$socket.registerCallBack('hotData', this.getData);
  },

  mounted() {
    this.initChart();

    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'hotData',
      chartName: 'hot',
      value: ''
    });

    window.addEventListener("resize", this.screenAdapter );
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    this.$socket.unrRegisterCallBack('hotData')
  },

  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, this.theme);
      const initOption = {
        title: {
          text: '▎ '+"热销商品销售金额占比统计",
          left: 20,
          top: 20,
        },
        tooltip: {
          show: true,
          formatter: (arg) => {
            return `
            ${arg.name}:${arg.percent}%
            `
          },
        },
        legend: {
          top: "15%",
          icon: "circle",
        },
        series: [
          {
            type: "pie",
            label: {
              show: false,
            },
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    getData(res) {
      // let res = await this.$http.get("hot");
      this.allData = res;
      this.updateChart();
    },
    updateChart() {
      const legendArr = this.allData[this.currtIndex].children.map((item) => {
        return item.name;
      });
      const seriesArr = this.allData[this.currtIndex].children.map((item) => {
        return {
          name: item.name,
          value: item.value,
        };
      });
      const updateOption = {
        legend: {
          data: legendArr,
        },
        series: [
          {
            data: seriesArr,
          },
        ],
      };
      this.chartInstance.setOption(updateOption);
    },

    // 分辨率变化
    screenAdapter() {
      // 计算当前字体大小
      this.titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6;
      const uptateOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          },
        },
        series: {
          radius:this.titleFontSize *4.5,
          center:['50%','60%']
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(uptateOption);
      this.chartInstance.resize();
    },
    toLeft() {
      this.currtIndex--;
      if (this.currtIndex < 0) {
        this.currtIndex = this.allData.length - 1;
      }
      this.updateChart();
    },
    toRight() {
      this.currtIndex++;
      if (this.currtIndex > this.allData.length - 1) {
        this.currtIndex = 0;
      }
      this.updateChart();
    },
  },
};
</script>

<style lang="less" scoped>
.arr-left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  color: #fff;
  cursor: pointer;
}
.arr-right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  color: #fff;
  cursor: pointer;
}
.name {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 20px;
  color: #fff;
}
</style>
